


% Bootstrapped Standard Errors for Classical Minimum Distance Estimator Basic Model 

clear
Model = 1
Version = 5


m = importdata('results/allboot.csv');
moments = array2table(m);



% Start at optimum of original model
StartPar = [ 43.08 ; 6.7493 ; 168.73 ; 41688 ; 76.406 ; 49.897 ]


n0 = 1;
nz = 500;

T0               = clock ;
Hour0            = T0(4);
Min0             = T0(5);
TimeStamp0       = [Hour0; Min0];

outputall       = ones(11,1);


for n = n0:nz
  
Repetition      = n

format shortg;
T               = clock ;
Hour            = T(4);
Min             = T(5);
TimeStamp       = [Hour, Min]

BootMoments     = moments{n,{'m1','m2', 'm3', 'm4','m5', 'm6', 'm7','m8' }};
BootMoments     = BootMoments';
W = [ 0.0001080 ; 0.0001452 ; 0.0001247 ; 0.0001223 ;  5.2188935 ; 4.8619146 ; 5.8740554 ; 2.6794026  ]
MomentCovar     = diag(W);

options = optimoptions(@fmincon,'Algorithm','sqp','TolFun', 1e-12, 'MaxIterations',20000, 'MaxFunEvals',20000)

lb = [0;0.1;0;0;0;0];
ub = [10;1000;100000;100000;100000;100000];

try

[BetaHat,fval,exitflag,output]     = fmincon(@(param) v5_LossFunction(param, BootMoments, MomentCovar), [StartPar],  [],[],[],[],lb,ub,[],options);
end

try
itervar = output.funcCount;
BetaHat = [BetaHat;itervar;fval; exitflag; Model; Version]; 
outputall       = [outputall, BetaHat];

end
end

Tz               = clock ;
Hourz            = Tz(4);
Minz             = Tz(5);
TimeStampz       = [Hourz; Minz];

sim = ones(12,1);
sim(1) = n0;
sim(2) = nz;
sim(3) = Hour0;
sim(4) = Min0 ; 
sim(5) = Hourz ;
sim(6) = Minz ;


outputall= outputall';
outputall([1],:) = [];
outputall = outputall(all(outputall,2),:);

csvwrite('results/v5_Bootstrap.csv',outputall);
